package com.kangye.exam.controller;

import com.kangye.exam.bean.Grade;
import com.kangye.exam.bean.Subject;
import com.kangye.exam.service.GradeService;
import com.kangye.exam.utils.JsonUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * @author 海风
 * 对年级表的增删改查操作
 * @data 2021-3-15 18:45:09
 * @since 1.1.5
 * */
@RestController
@RequestMapping("/grade")
@CrossOrigin
public class GradeController {

    @Resource
    private GradeService gradeService;

    /*
     * 查询所有年级
     *
     * */
    @RequestMapping("/find_all")
    public Object find_all() {
        List<Grade> list = gradeService.findAll();
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("data", list);
        return map;
    }

    /*
     * 通过id查找年级
     * @id 学科id
     * */
    @RequestMapping("/find/{id}")
    public Object find_id(@PathVariable("id") Integer id) {
        Map<String, Object> map = new HashMap<>();
        Grade grade = gradeService.selectById(id);
        map.put("code",0);
        map.put("data",grade);
        return map;
    }

    /*
     * 插入一条年级
     * @grade 学科class
     * */
    @RequestMapping("/insert")
    public Object insert(Grade grade) {
        return gradeService.insert(grade)>0?"{\"code\":0}":"{\"code\":-1}";
    }

    /*
     * 修改一条年级
     * @grade 学科class,判空
     * */
    @RequestMapping("/update")
    public Object update(Grade grade) {
        return gradeService.update(grade)>0?"{\"code\":0}":"{\"code\":-1}";
    }

    /*
     * 删除一条年级
     * @id 需要删除的学科id
     * */
    @RequestMapping("/delete/{id}")
    public Object delete(@PathVariable("id") Integer id) {
        if (gradeService.delete(id)>0){
            return "{\"code\":0}";
        }
        return "{\"code\":-1}";
    }
}
